<template>
<div>
<!--  修改OR添加  -->
<!--  <el-page-header @back="goBack" title="公告通知" content="编辑" ></el-page-header>-->
  <!--  分割线  -->
  <el-divider class="head"></el-divider>

  <div style="width: 60%">
  <div style="margin: 20px;"></div>
  <el-form label-position="top" label-width="80px" :model="AdmNoticeForm">
    <el-form-item label="标题:">
      <el-input v-model="AdmNoticeForm.title"></el-input>
    </el-form-item>

    <el-form-item label="通知结束时间:">
      <el-date-picker
          v-model="AdmNoticeForm.noticeNotificationendtime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss "
          type="datetime"
          placeholder="选择日期时间">
      </el-date-picker>
    </el-form-item>

    <el-form-item label="相关文件:">

      <el-upload
          style="height: 10px"
          ref="upload"
          action=""
          :before-upload="beforeUpload"
          :http-request="addFile"
          :limit=1>
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>

      <div style="height: 30px"></div>

    </el-form-item>

    <el-form-item label="通知内容:">
      <el-input
          type="textarea"
          :rows="10"
          v-model="AdmNoticeForm.noticeContent">
      </el-input>
    </el-form-item>

<!--    <el-form-item>-->
<!--      <el-button @click="upOrAddAdmNotice">确认提交</el-button>-->
<!--    </el-form-item>-->

    <el-row>
      <el-col :span="5">
        <el-form-item>
          <el-button @click="upOrAddAdmNotice">确认提交</el-button>
        </el-form-item>
      </el-col>

      <el-col :span="5">
        <el-form-item>
          <el-button size="medium" type="primary" plain @click="goBack" >返回</el-button>
        </el-form-item>
      </el-col>

    </el-row>

  </el-form>


  </div>


</div>
</template>

<script>
export default {
  name: "UpdateAdmNotice",
  data(){
    return{
      file:"",
      fileName:"",
      AdmNoticeForm:{
        noticeReleasetime:"",// 发布日期
        noticeNotificationtime:"", // 通知时间
        noticeFile:"",// 附带文件
        systemUserId: JSON.parse(localStorage["user"])[0].id , // 当前登陆者
      },// 添加的数据
    }
  },
  created() {
this.init()
  },
  methods:{
    init(){
      if(this.$route.params && this.$route.params.id){ // 如果跳页面时有id 为修改
        this.getAdmNoticeByid();
      }
    },
    getAdmNoticeByid(){ // 根据id查询  修改前数据

      const loading = this.$loading({
        lock: true,
        text: '正在载入...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      this.$http.get(`admNotice/${this.$route.params.id}`)
      .then(res => {
        setTimeout(() => {
          loading.close();
        });

        this.AdmNoticeForm = res.data.data
      })
      .catch()
    },
    beforeUpload(file){
      this.file=file
      this.fileName=file.name
    },
    addFile(){},
    goBack() {
      this.$router.go(-1)
    },// 返回按钮
    upOrAddAdmNotice(){
      // this.AdmNoticeForm.noticeNotificationtime =new Date().toLocaleDateString();// 获取通知日期
      // this.AdmNoticeForm.noticeReleasetime=new Date().toLocaleDateString()

      this.AdmNoticeForm.noticeNotificationtime = this.$date.formatDate(new Date(),"yyyy-MM-DD hh:mm:ss")// 获取通知日期
      this.AdmNoticeForm.noticeReleasetime = this.$date.formatDate(new Date(),"yyyy-MM-DD hh:mm:ss")

      if(this.$route.params && this.$route.params.id){ // 有id传入为修改

        if(this.file != '' & this.file != null & this.fileName != '' & this.fileName != ''){
          let formatData=new FormData(); // 点击添加按钮先上传文件 成功回调获取文件地址并添加到表
          formatData.append('file',this.file,this.fileName)
          let requestConfig={
            headers:{
              'Content-Type':'multipart/form-data'
            }
          }
          this.$http.post("file", formatData,requestConfig)
              .then(res => {
                this.AdmNoticeForm.noticeFile = res.data.data
                this.upFun()
              })
              .catch()
        }else {
          this.upFun()
        }

      }else {

        if (this.file != '' & this.file != null & this.fileName != '' & this.fileName != '') {
          let formatData = new FormData(); // 点击添加按钮先上传文件 成功回调获取文件地址并添加到表
          formatData.append('file', this.file, this.fileName)
          let requestConfig = {
            headers: {
              'Content-Type': 'multipart/form-data'
            }
          }
          this.$http.post("file", formatData, requestConfig)
              .then(res => {
                this.AdmNoticeForm.noticeFile = res.data.data
                this.addFun()
              })
              .catch()
        } else {
          this.addFun()
        }
      }
    },
    addFun(){ // 添加方法

      const loading = this.$loading({
        lock: true,
        text: '正在载入...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      this.$http.post("admNotice",this.AdmNoticeForm)
      .then( () => {

        setTimeout(() => {
          loading.close();
        });

        this.$message({
          message: '添加成功!',
          type: 'success'
        });
        this.$router.go(-1)
      })
      .catch()
    },
    upFun(){ // 修改方法
      const loading = this.$loading({
        lock: true,
        text: '正在载入...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      this.$http.put("admNotice",this.AdmNoticeForm)
          .then( () => {
            setTimeout(() => {
              loading.close();
            });
            this.$message({
              message: '修改成功!',
              type: 'success'
            });
            this.$router.go(-1)
          })
          .catch()

    },

  }
}
</script>

<style scoped>
.head{
  margin-top: 10px ;
  margin-bottom: 15px ;
  height: 2px;
}
</style>